package com.ews.onlineexchange.dao;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.Transient;

import com.ews.onlineexchange.model.QualificationCert;
import com.ews.onlineexchange.model.QualificationTemplate;
import com.ews.onlineexchange.model.ServiceSupplier;
import com.ews.onlineexchange.model.TradeRating;

@Stateless
public class ServiceSupplierDao implements Serializable {
	
	@Inject
	EntityManager entityManager;
	
	private static final String FIND_BY_SEQNO_EQL = ""
			+ "	SELECT		ss"
			+ "	FROM		ServiceSupplier ss "
			+ "	WHERE		ss.seqNo = :seqNo	";
	public ServiceSupplier findBySeqNo(String seqNo) {
		Query query = entityManager.createQuery(FIND_BY_SEQNO_EQL);
		query.setParameter("seqNo", seqNo);
		List<ServiceSupplier> resultList = query.getResultList();
		if (resultList.size() == 0)
			return null;
		ServiceSupplier result = resultList.get(0);
		result = getInitializedInstance(result);
		return result;
	}
	
	public ServiceSupplier getInitializedInstance(ServiceSupplier instance) {
		if (!entityManager.contains(instance))
			instance = entityManager.merge(instance);
		instance.getHistoryRejectRecords().size();
		instance.getMainBusinesses().size();
		instance.getAttachments().size();
		instance.getTaxProveImages().size();
		instance.getBusinessLicenseImages().size();
		for (QualificationCert cert : instance.getCerts()) {
			cert.getHistoryRejectRecords().size();
			cert.getQualificationTemplates().size();
			for (QualificationTemplate qt : cert.getQualificationTemplates()) {
				qt.getSectors().size();
			}
		}
		return instance;
	}

	public ServiceSupplier find(String id) {		
		return getInitializedInstance(entityManager.find(ServiceSupplier.class, id));
	}
	
	
}
